package com.wlyuan.open.controller;

import com.wlyuan.core.exception.BaseException;
import com.wlyuan.ere.api.DeviceCmdService;
import com.wlyuan.ere.api.DeviceImportService;
import com.wlyuan.ere.api.EreService;
import com.wlyuan.ere.api.TmsDevicesStandService;
import com.wlyuan.ere.api.mongo.PositionService;
import com.wlyuan.ere.api.mongo.StatusService;
import com.wlyuan.ere.common.domain.TmsDevicesStand;
import com.wlyuan.ere.common.dto.*;
import com.wlyuan.ere.common.filter.DeviceFilter;
import com.wlyuan.ere.common.filter.DeviceSearchFilter;
import com.wlyuan.ere.common.filter.PositionSearchFilter;
import com.wlyuan.ere.common.vo.PositionVO;
import com.wlyuan.ere.common.vo.StatusVO;
import com.wlyuan.ere.push.api.DeviceRateSetService;
import com.wlyuan.ere.push.common.dto.DeviceRateDTO;
import com.wlyuan.open.annotation.OpenApi;
import com.wlyuan.tools.Page;
import com.wlyuan.web.claim.ClaimUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author 设备相关
 */
@Api(tags = "设备相关")
@Validated
@RequestMapping("/api/device")
@RestController
public class OpenDeviceController {
    @DubboReference(check = false)
    private PositionService positionService;
    @DubboReference(check = false)
    private EreService ereService;
    @DubboReference(check = false)
    private DeviceCmdService deviceCmdService;
    @DubboReference(check = false)
    private StatusService statusService;
    @DubboReference(check = false)
    private DeviceRateSetService deviceRateSetService;
    @DubboReference(check = false)
    private DeviceImportService deviceImportService;
    @DubboReference(check = false)
    private TmsDevicesStandService tmsDevicesStandService;

    @ApiOperation("获取设备最新定位信息")
    @GetMapping("/position/last/{deviceCode}")
    @OpenApi
    public PositionVO getLastPosition(@Valid @NotNull @PathVariable String deviceCode) {
        return positionService.getLastPosition(deviceCode);
    }

    @ApiOperation("获取指定范围内设备定位信息")
    @PostMapping("/position")
    @OpenApi
    public List<PositionVO> findByDeviceCodeAndTime(@Valid @RequestBody PositionSearchFilter filter) {
        return positionService.findByDevCodeAndTime(filter);
    }

    @ApiOperation("获取设备列表")
    @PostMapping("/list")
    @OpenApi
    public List<DeviceSearchDTO> getDeviceList(@Valid @RequestBody DeviceSearchFilter filter) {
        filter.setTenantId(filter.getTenantId());
        return ereService.getDeviceList(filter);
    }

    @ApiOperation("设备管理--设备列表(带分页)")
    @PostMapping("/search")
    public Page<DeviceExportDTO> deviceList(@Valid @RequestBody DeviceFilter deviceFilter) {
        deviceFilter.setCompanyId(ClaimUtils.get().getTenantId());
        return ereService.deviceExportList(deviceFilter);
    }

    @ApiOperation("设备管理--设备数量统计")
    @GetMapping("/getCount")
    public DeviceCountDTO deviceCount() {
        return ereService.getDeviceCount(ClaimUtils.get().getTenantId());
    }

    @ApiOperation(value = "查询设备最新状态信息")
    @GetMapping(value = "/status/last/{deviceCode}")
    @OpenApi
    public StatusVO getLastStatus(@NotNull @PathVariable String deviceCode) {
        return statusService.getLastStatus(deviceCode);
    }

    @ApiOperation(value = "大田设置电子回单开始、结束追踪、自动解绑")
    @PostMapping(value = "/{event}/rate")
    @OpenApi
    public boolean insert(@PathVariable String event, @Valid @RequestBody DeviceRateDTO deviceRateDTO) {
        deviceRateDTO.setEvent(event);
        return deviceRateSetService.setDeviceRate(deviceRateDTO);
    }

    @ApiOperation(value = "设置设备定位频率")
    @GetMapping(value = "/settingRate/{deviceCode}/{rate}")
    @OpenApi
    public DeviceCmdResultDTO settingRate(@NotNull @PathVariable String deviceCode,
                                          @NotNull @PathVariable Integer rate) {
        return deviceCmdService.settingRate(deviceCode, 4, null, rate);
    }

    @PostMapping("/setInHome/readExcel")
    @ApiOperation(value = "设备导入--设备是否在公司导入读取excel")
    public List<DeviceInHomeImportDTO> readExcel(@RequestPart("file") MultipartFile file)
            throws IOException {
        return deviceImportService
                .process(file.getBytes(), ClaimUtils.get().getUserId(), ClaimUtils.get().getTenantId());
    }

    @PostMapping("/batchInsert")
    @ApiOperation(value = "设备导入--设备是否在公司导入")
    public List<DeviceInHomeImportDTO> batchInsert(@RequestBody List<DeviceInHomeImportDTO> excelItems) {
        if (!deviceImportService.validate(excelItems, ClaimUtils.get().getUserId(), ClaimUtils.get().getTenantId())) {
            return excelItems;
        }
        deviceImportService.batchInsert(excelItems);
        return excelItems;
    }

    @GetMapping("/setInHome/{devCode}/{inHome}")
    @ApiOperation(value = "设备导入--调整设备是否在公司")
    public int setInHome(@PathVariable String devCode, @PathVariable Integer inHome) {
        return tmsDevicesStandService.setInHome(devCode, inHome, ClaimUtils.get().getTenantId());
    }


    @ApiOperation(value = "根据设备号获取单设备信息(含状态数据);positionFlag:0:未返回状态数据,1:返回定位数据")
    @GetMapping(value = "/getDeviceInfo/{deviceCode}/{positionFlag}")
    public TmsDevicesDTO getByDeviceCode(@PathVariable("deviceCode") String deviceCode,
                                         @PathVariable("positionFlag") Integer positionFlag) {
        TmsDevicesStand tmsDevicesStand = tmsDevicesStandService
                .getByDevCode(deviceCode, ClaimUtils.get().getTenantId());
        if (null == tmsDevicesStand) {
            throw new BaseException(10010, "设备不存在");
        }
        return ereService.getByDevCode(tmsDevicesStand, positionFlag);
    }


}
